//
//  JFBSettlementStatistics.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/21.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "JFBSettlementStatisticsDS.h"

@implementation JFBSettlementStatisticsDS

- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }
    self.date = [[attributes objectForKey: @"today"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"today"];
    self.merchandiseCount = [[attributes objectForKey: @"product_quantity"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"product_quantity"]  integerValue];
    self.orderCount = [[attributes objectForKey: @"so_quantity"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"so_quantity"]  integerValue];
    self.price = [[attributes objectForKey: @"total_price"] isEqual: [NSNull null]] ? 0 : [NSString formatDecimal:[attributes objectForKey:@"total_price"]];
    
    return self;
}


- (void)buildWithMockAttributes {
    self.date = @"2015-12-04";
    self.merchandiseCount = 12;
    self.orderCount = 30;
    self.price = [NSDecimalNumber decimalNumberWithString:@"859.5"];
}

+ (NSURLSessionDataTask *)loadStatisticsList:(void (^)(NSArray *statisticsList, NSError *error))block pager:(JFBPager*)pager {
    
    return [[JFBAPIClient sharedClient:true] GET:@"/shop/balance/day.json" parameters:nil success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"error"];
        if( error == [NSNull null] ) {
            NSMutableArray *result = [@[] mutableCopy];
            NSArray *response = [JSON valueForKeyPath:@"data"];
            for( NSDictionary *item in response ) {
                [result addObject:[[JFBSettlementStatisticsDS alloc] initWithAttributes:item]];
            }
            block([NSArray arrayWithArray:result], nil);
        } else {
            block(nil, nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, error);
    }];
    return nil;
}
@end
